package com.gadarts.parashoot.enemies;

import java.util.ArrayList;

import android.graphics.Bitmap;

import com.gadarts.parashoot.GameModel;
import com.gadarts.parashoot.GameObject;
import com.gadarts.parashoot.GameView;
import com.gadarts.parashoot.Sprite;

public abstract class Enemy extends GameObject {

	protected int health;
	private Bitmap partImage;
	protected boolean dead;
	protected String deadTimer;// The timer of the dead body.

	public Enemy(GameModel model, GameView view, ArrayList<Sprite> sprites, int x,
			int y, int speed, int direction, int depth, int health,
			boolean fitFrameToDirection, Bitmap partImage) {
		super(model, view, sprites, x, y, speed, direction, depth,
				fitFrameToDirection);
		this.health = health;
		this.partImage = partImage;
	}
	
	public Enemy(GameModel model, GameView view, Sprite sprite, int x,
			int y, int speed, int direction, int depth, int health,
			boolean fitFrameToDirection, Bitmap partImage) {
		super(model, view, sprite, x, y, speed, direction, depth,
				fitFrameToDirection);
		this.health = health;
		this.partImage = partImage;
	}

	public int getHealth() {
		return health;
	}

	public void setHealth(int newHealth) {
		health = newHealth;
	}

	// Creates random number of flying parts flying of the enemy.
	protected void createRandomFlyingParts(int number, boolean towardsTop) {
		int numberOfParts = random.nextInt(number) + 2;
		// Create flying part all over the enemy.
		for (int i = 0; i < numberOfParts; i++) {
			int randomX = random.nextInt(currentImage.getWidth());
			model.createFlyingPart(x + randomX, y, partImage, towardsTop);
		}
	}

	// Blow enemy if it is too damaged.
	protected void checkIfTooDamaged(int explosionDamageAmount) {
		if (health <= -explosionDamageAmount) {
			createRandomFlyingParts(8, true);
			model.createExplosion(getCenterX(), getCenterY(), 1);
			model.destroyObject(this, getClass());
		}
	}

	// Whatever happens when health's gone.
	protected void checkHealth() {
		if(health <= 0)
			if(!dead)
				die();
	}

	protected void die() {
		dead = true;
	}
}
